package org.zfes.snowier.zbss.xzy.biz.device.service.impl;

import java.util.Map;
import java.util.Optional;
import org.zfes.snowier.core.data.DataSet;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.zfes.snowier.core.util.ZBeanUtil;
import org.zfes.snowier.core.util.ZDateUtil;
import org.zfes.snowier.core.util.ZAlert;
import org.zfes.snowier.core.util.ZAssert;
import org.zfes.snowier.core.data.ParamMap;
import org.zfes.snowier.zbss.xzy.biz.device.dao.DeviceMapper;
import org.zfes.snowier.zbss.xzy.biz.device.model.Device;
import org.zfes.snowier.zbss.xzy.biz.device.service.IDeviceService;

import java.util.stream.Stream;
@Service
public class DeviceServiceImpl implements IDeviceService{
 	@Autowired
	private DeviceMapper deviceMapper;
	
	@Transactional(rollbackFor=Exception.class)
 	@Override
	public void createDevice(Device device) {
		ZBeanUtil.setCreateInfo(device);
		ZBeanUtil.validateBean(device);
		
		Object id2=deviceMapper.selectIdByDeviceNum(device.getDeviceNum());
		if(id2!=null){
			ZAlert.Error("序列号已经存在");
		}
		deviceMapper.insertSelective(device);
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public Optional<Device> loadDeviceById(Long id) {
		if(id!=null){
			return Optional.ofNullable(deviceMapper.selectByPrimaryKey(id));
		}
		return Optional.empty();
		
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void updateDevice(Device device) {
		ZBeanUtil.setUpdatedInfo(device);
		ZBeanUtil.validateBean(device);
		Object id2=deviceMapper.selectIdByDeviceNum(device.getDeviceNum());
		if(id2!=null&&!id2.equals(device.getId())){
			ZAlert.Error("序列号已经存在");
		}
		deviceMapper.updateByPrimaryKeySelective(device);
	}
	
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void deleteDevice(Long id) {
		ZAssert.notNull(id, "请选择要操作的数据");
		deviceMapper.deleteByPrimaryKey(id);
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void setDeviceStatus(Long id, Byte status) {
		ZAssert.notNull(id, "请选择要操作的数据");
		ZAssert.notNull(status, "状态参数错误");
		if(!Stream.of(new Byte[] {-1,0,1}).anyMatch(val->val.equals(status))) {
			ZAlert.serviceLogicalError("状态参数错误");
		}
		deviceMapper.updateStatus( id,  status) ;
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public DataSet loadDeviceDataSet(Map<String, Object> params) {
		ParamMap pm=ParamMap.filterParam(params);
		pm.getObj("name").ifPresent(v->pm.updateParam("name", "%"+v+"%"));
		pm.getObj("deptTreeCode").ifPresent(v->pm.updateParam("deptTreeCode", "%"+v+"%"));
		DataSet ds=DataSet.newDS2(deviceMapper.selectListCount(pm), deviceMapper.selectMapListPage(pm));
		return ds;
	}
	
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public Optional<Device> loadByDeviceNum(java.lang.String deviceNum){
		if(deviceNum!=null){
			return Optional.ofNullable(deviceMapper.selectByDeviceNum(deviceNum));
		}
		return Optional.empty();
	}

}